import { getRequest, postRequest } from '../../../api/baseRequest'

export default {
  props: {
    value: null
  },
  data() {
    return {
      phoneValidateCodeBtnText: '验证码',
      phoneValidateCodeBtn: false,
      open: false,
      inputValue: null,
      codeIsTrue: false,
      codeBool: true,
      phoneValidateForm: {
        phoneNumber: '',
        code: ''
      }
    }
  },
  methods: {
    codeCheck() {
      if (this.phoneValidateForm.code != '' && this.phoneValidateForm.code != null) {
        this.codeBool = false
      }
    },
    openDialogHandle() {
      this.open = true
      this.phoneValidateForm.code = ''
    },
    validateCodeHandle() {
      const { code } = this.phoneValidateForm
      if (code !== '' && code !== null) {
        this.$refs.form.validate((valid) => {
          if (valid) {
            // 调接口 验证成功
            postRequest('/form/common/phone/code/check', this.phoneValidateForm).then(({ data }) => {
              //this.phoneValidateForm.phoneNumber = res.data
              this.open = false
              this.$emit('input', data)
              this.codeIsTrue = true
            })
          } else {
            valid = false
          }
          return valid
        })
      }
    },
    sendValidateMsgHandle() {
      //手机号正则
      var mPattern = /^(?:0|86|\+86)?1[3456789]\d{9}$/
      if (mPattern.test(this.phoneValidateForm.phoneNumber)) {
        console.log('开始发送')
        this.phoneValidateForm.code = ''
        this.$refs.form.validateField('phoneNumber', (err) => {
          if (!err) {
            this.open = true
            this.phoneValidateCodeBtn = true
            getRequest(`/form/common/phone/code?phoneNumber=${this.phoneValidateForm.phoneNumber}`).then(() => {
              //this.msgSuccess('验证码发送成功，5分钟内有效')
              let count = 60
              const timer = setInterval(() => {
                count--
                this.phoneValidateCodeBtnText = `${count}s`
                if (count === 0) {
                  this.phoneValidateCodeBtnText = '验证码'
                  this.phoneValidateCodeBtn = false
                  clearInterval(timer)
                }
              }, 1000)
            })
          }
        })
      }
    }
  }
}
